package com.cxj.cxj_test_cloud.utils;

import android.app.Application;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class Logger {
    private static String fileName = MessageDigestHelper.MD5_DIGEST_HEX("yoyolog.properties");
    public static boolean DEBUG_ON = false;
    private static String propertyPath;
    private static final String TAG = "min-77";

    static {
        loadLogConfig();
    }

    private static void loadLogConfig() {
        Application application = SswlApplication.getAppComponent().getApplication();
        try {
            if (application != null){
                String switchOn = Settings.Secure.getString(application.getContentResolver(), fileName);
                if ("debug".equals(switchOn)){
                    setDebug();
                    Log.i("cxj", "Logger: debug");
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            propertyPath = application.getExternalFilesDir(null).getAbsolutePath() + File.separator + "min-77" + File.separator + fileName;
            Properties property = readProperty(propertyPath);
            if (property != null){
                if ("true".equals(property.getProperty("showLog", "false"))){
                    DEBUG_ON=true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void d(String tag, String msg) {
        if (DEBUG_ON) {
            Log.d(tag, msg);
        }
    }

    public static void e(String tag, String msg) {
        if (DEBUG_ON) {
            Log.e(tag, msg);
        }
    }

    public static void i(String tag, String msg) {
        if (DEBUG_ON) {
            Log.i(tag, msg);
        }
    }

    public static void w(String tag, String msg) {
        if (DEBUG_ON) {
            Log.w(tag, msg);
        }
    }

    public static void d(String msg){
        if (DEBUG_ON){
            Log.d(TAG, msg);
        }
    }

    public static void e(Throwable e){
        if (DEBUG_ON){
            if (e != null){
                StringBuffer sb = new StringBuffer();
                StackTraceElement[] stackTrace = e.getStackTrace();
                if (stackTrace.length>0){
                    for (StackTraceElement stackTraceElement : stackTrace) {
                        sb.append(stackTraceElement.toString());
                        sb.append("\n");
                    }
                }
                Log.e(TAG, "异常错误为："+e+"\n"+sb);
            }
        }
    }

    public static void i(String msg){
        if (DEBUG_ON){
            Log.i(TAG, msg);
        }
    }

    public static void w(String msg){
        if (DEBUG_ON){
            Log.w(TAG, msg);
        }
    }

    private static Properties readProperty(String propertyPath) {
        if (TextUtils.isEmpty(propertyPath)){
            return null;
        }
        File file=new File(propertyPath);
        if (file.isFile() && file.exists()){
            try {
                FileInputStream fis=new FileInputStream(file);
                Properties properties=new Properties();
                properties.load(fis);
                return properties;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static void setDebug() {
        DEBUG_ON = true;
        writerProperty(propertyPath);
    }

    private static void writerProperty(String propertyPath) {
        if (TextUtils.isEmpty(propertyPath)){
            return;
        }
        File file=new File(propertyPath);
        if (!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        try {
            FileWriter fw=new FileWriter(file);
            fw.write("showLog=true");
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
